//
//  PlatformUserManagement.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/11.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "PlatformUserManagement.h"
#import "ChineseString.h"
#import "TalkingViewController.h"
#import "Session.h"
@interface PlatformUserManagement ()
{
    UITableView *_tableView;
    NSMutableArray *stringsToSort;     /**< 存放当前数据源 */
    NSMutableArray *Datasource;
}
@end

@implementation PlatformUserManagement
@synthesize indexArray;
@synthesize LetterResultArr;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"用户管理";


    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    _tableView.sectionIndexColor = [UIColor colorWithRed:142/255.0 green:142/255.0 blue:142/255.0 alpha:0.9];
    [self.view addSubview:_tableView];
    stringsToSort = [NSMutableArray array];
    Datasource = [NSMutableArray array];
    [self loadData];
    
   
    NSMutableDictionary *parame = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"fuid",@"855",@"接口接口，快点实现吧",@"word_content",nil];
//    [HTTPGlobals shareHTTPGlobal].manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    [HTTPGlobals shareHTTPGlobal].manager. requestSerializer = [ AFHTTPRequestSerializer serializer ];
//    
//    [HTTPGlobals shareHTTPGlobal].manager. responseSerializer = [ AFHTTPResponseSerializer serializer ];
    //实现与xx聊天 普通消息
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:@"MobilePort/Message/xx_add_word.html" parameters:parame finish:^(BOOL seccess, NSString *error, id obj) {
        if (seccess) {
            DLog(@"obj = %@",obj);
            DLog(@"info = %@",obj[@"info"]);
        }else {
            DLog(@"卧槽又失败了,什么鬼接口!");
        }
    }];
    //检查用户是否接受公众号消息
//    [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:@"MobilePort/Message/check_user_sub.html" parameters:parame finish:^(BOOL seccess, NSString *error, id obj) {
//        if (seccess) {
//            DLog(@"是否关注公众号 : %@",obj);
//            DLog(@"info = %@",obj[@"info"]);
//        }else{
//            DLog(@"失败了!");
//        }
//    }];
}
//加载数据源
-(void)loadData {
    //获取用户列表信息
    [self.HUD show:YES];
    [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:@"MobilePort/Usersub/index.html" parameters:nil finish:^(BOOL seccess, NSString *error, id obj) {
        [self.HUD hide:YES];
        if (seccess) {
            DLog(@"请求成功用户数据成功!");
            DLog(@"obj______ = %@",obj);
            DLog(@"obj____ = %@",obj[@"usersub_info"]);
            for (NSDictionary *dic in obj[@"usersub_info"][@"data"]) {
                [stringsToSort addObject:dic[@"nickname"]];
                NSMutableDictionary *UserDic = [NSMutableDictionary dictionary];
                [UserDic setValue:dic[@"nickname"] forKey:@"nickname"];
                [UserDic setValue:dic[@"uid"] forKey:@"uid"];
                [Datasource addObject:UserDic];
            }
            self.indexArray = [ChineseString IndexArray:stringsToSort];
            self.LetterResultArr = [ChineseString LetterSortArray:stringsToSort];
            [_tableView reloadData];
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
            
        }
    }];
}

#pragma mark -Section的Header的值
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.indexArray objectAtIndex:section];
//    NSString * regularExpression = @"^[A-Za-z]+$";
//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regularExpression];
//    BOOL flag = [pre evaluateWithObject:key];
//    if (flag) {
//        return key;
//    }else {
//        return @"#";
//    }
    return key;
}
#pragma mark - Section header view(每一层的view);
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    lab.backgroundColor = [UIColor grayColor];
    lab.text = [NSString stringWithFormat:@"  %@",[self.indexArray objectAtIndex:section]];
    lab.textColor = [UIColor blackColor];
    return lab;
}
#pragma mark - row height(单元格的高)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55.0;
}

#pragma mark -
#pragma mark Table View Data Source Methods
#pragma mark -设置右方表格的索引数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.indexArray;
}
#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    DLog(@"title===%@",title);
    return index;
}

#pragma mark -允许数据源告知必须加载到Table View中的表的Section数。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.indexArray count];
}
#pragma mark -设置表格的行数为数组的元素个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.LetterResultArr objectAtIndex:section] count];
}
#pragma mark -每一行的内容为数组相应索引的值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.text = [[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    return cell;
}
#pragma mark - Select内容为数组相应索引的值
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"---->%@",[[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    for (NSDictionary *dic in Datasource) {
        if ([dic[@"nickname"] isEqualToString:self.LetterResultArr[indexPath.section][indexPath.row]]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:[NSString stringWithFormat:@"%@的uid是%@",dic[@"nickname"],dic[@"uid"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            User *user =[User setUserModel:dic[@"uid"]];
            DLog(@"user.nickname = %@",user.nickname);
            DLog(@"user.uid = %@",user.uid);
            DLog(@"dic.uid = %@",dic[@"uid"]);
            
            Session * session = [Session getSessionWithID:dic[@"uid"]];
            if (!session) {
                session = [Session sessionWithUser:user];
            }
            DLog(@"session = %@",session);
            DLog(@"uid = %@",session.uid);
            DLog(@"name = %@",session.name);
            DLog(@"content = %@",session.content);
            DLog(@"headsmall = %@",session.headsmall);
            DLog(@"message = %@",session.message);
            DLog(@"typechat = %d",session.typechat);
            DLog(@"unreadCount = %d",session.unreadCount);
            DLog(@"istop = %d",session.istop);
            DLog(@"isshownick = %d",session.isshownick);
            DLog(@"issubPlat = %d",session.isSubPlat);
            TalkingViewController * con = [[TalkingViewController alloc] initWithSession:session];
            [self pushViewController:con];
            
        }
    }
    
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
//                                                    message:[[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]
//                                                   delegate:nil
//                                          cancelButtonTitle:@"YES" otherButtonTitles:nil];
//    [alert show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
